<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Form fields &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../index.html" />
    <link rel="up" title="Forms" href="index.html" />
    <link rel="next" title="Model Form Functions" href="models.html" />
    <link rel="prev" title="The Forms API" href="api.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="api.html" title="The Forms API">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="models.html" title="Model Form Functions">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-forms-fields">
            
  <div class="section" id="s-module-django.forms.fields">
<span id="s-form-fields"></span><span id="module-django.forms.fields"></span><span id="form-fields"></span><h1>Form fields<a class="headerlink" href="#module-django.forms.fields" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="django.forms.Field">
<em class="property">class </em><tt class="descname">Field</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.Field" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>When you create a <tt class="docutils literal"><span class="pre">Form</span></tt> class, the most important part is defining the
fields of the form. Each field has custom validation logic, along with a few
other hooks.</p>
<dl class="method">
<dt id="django.forms.Field.clean">
<tt class="descclassname">Field.</tt><tt class="descname">clean</tt>(<em>value</em>)<a class="headerlink" href="#django.forms.Field.clean" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Although the primary way you&#8217;ll use <tt class="docutils literal"><span class="pre">Field</span></tt> classes is in <tt class="docutils literal"><span class="pre">Form</span></tt> classes,
you can also instantiate them and use them directly to get a better idea of
how they work. Each <tt class="docutils literal"><span class="pre">Field</span></tt> instance has a <tt class="docutils literal"><span class="pre">clean()</span></tt> method, which takes
a single argument and either raises a <tt class="docutils literal"><span class="pre">django.forms.ValidationError</span></tt>
exception or returns the clean value:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">EmailField</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="s">&#39;foo@example.com&#39;</span><span class="p">)</span>
<span class="go">u&#39;foo@example.com&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="s">&#39;invalid email address&#39;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">ValidationError</span>: <span class="n">[u&#39;Enter a valid email address.&#39;]</span>
</pre></div>
</div>
<div class="section" id="s-core-field-arguments">
<span id="s-id1"></span><span id="core-field-arguments"></span><span id="id1"></span><h2>Core field arguments<a class="headerlink" href="#core-field-arguments" title="Permalink to this headline">¶</a></h2>
<p>Each <tt class="docutils literal"><span class="pre">Field</span></tt> class constructor takes at least these arguments. Some
<tt class="docutils literal"><span class="pre">Field</span></tt> classes take additional, field-specific arguments, but the following
should <em>always</em> be accepted:</p>
<div class="section" id="s-required">
<span id="required"></span><h3><tt class="docutils literal"><span class="pre">required</span></tt><a class="headerlink" href="#required" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.forms.Field.required">
<tt class="descclassname">Field.</tt><tt class="descname">required</tt><a class="headerlink" href="#django.forms.Field.required" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>By default, each <tt class="docutils literal"><span class="pre">Field</span></tt> class assumes the value is required, so if you pass
an empty value &#8211; either <tt class="docutils literal"><span class="pre">None</span></tt> or the empty string (<tt class="docutils literal"><span class="pre">&quot;&quot;</span></tt>) &#8211; then
<tt class="docutils literal"><span class="pre">clean()</span></tt> will raise a <tt class="docutils literal"><span class="pre">ValidationError</span></tt> exception:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="s">&#39;foo&#39;</span><span class="p">)</span>
<span class="go">u&#39;foo&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">ValidationError</span>: <span class="n">[u&#39;This field is required.&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">ValidationError</span>: <span class="n">[u&#39;This field is required.&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="s">&#39; &#39;</span><span class="p">)</span>
<span class="go">u&#39; &#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="go">u&#39;0&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>
<span class="go">u&#39;True&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
<span class="go">u&#39;False&#39;</span>
</pre></div>
</div>
<p>To specify that a field is <em>not</em> required, pass <tt class="docutils literal"><span class="pre">required=False</span></tt> to the
<tt class="docutils literal"><span class="pre">Field</span></tt> constructor:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">required</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="s">&#39;foo&#39;</span><span class="p">)</span>
<span class="go">u&#39;foo&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">)</span>
<span class="go">u&#39;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>
<span class="go">u&#39;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="go">u&#39;0&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>
<span class="go">u&#39;True&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
<span class="go">u&#39;False&#39;</span>
</pre></div>
</div>
<p>If a <tt class="docutils literal"><span class="pre">Field</span></tt> has <tt class="docutils literal"><span class="pre">required=False</span></tt> and you pass <tt class="docutils literal"><span class="pre">clean()</span></tt> an empty value,
then <tt class="docutils literal"><span class="pre">clean()</span></tt> will return a <em>normalized</em> empty value rather than raising
<tt class="docutils literal"><span class="pre">ValidationError</span></tt>. For <tt class="docutils literal"><span class="pre">CharField</span></tt>, this will be a Unicode empty string.
For other <tt class="docutils literal"><span class="pre">Field</span></tt> classes, it might be <tt class="docutils literal"><span class="pre">None</span></tt>. (This varies from field to
field.)</p>
</div>
<div class="section" id="s-label">
<span id="label"></span><h3><tt class="docutils literal"><span class="pre">label</span></tt><a class="headerlink" href="#label" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.forms.Field.label">
<tt class="descclassname">Field.</tt><tt class="descname">label</tt><a class="headerlink" href="#django.forms.Field.label" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The <tt class="docutils literal"><span class="pre">label</span></tt> argument lets you specify the &#8220;human-friendly&#8221; label for this
field. This is used when the <tt class="docutils literal"><span class="pre">Field</span></tt> is displayed in a <tt class="docutils literal"><span class="pre">Form</span></tt>.</p>
<p>As explained in &#8220;Outputting forms as HTML&#8221; above, the default label for a
<tt class="docutils literal"><span class="pre">Field</span></tt> is generated from the field name by converting all underscores to
spaces and upper-casing the first letter. Specify <tt class="docutils literal"><span class="pre">label</span></tt> if that default
behavior doesn&#8217;t result in an adequate label.</p>
<p>Here&#8217;s a full example <tt class="docutils literal"><span class="pre">Form</span></tt> that implements <tt class="docutils literal"><span class="pre">label</span></tt> for two of its fields.
We&#8217;ve specified <tt class="docutils literal"><span class="pre">auto_id=False</span></tt> to simplify the output:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">CommentForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">name</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="s">&#39;Your name&#39;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">url</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">URLField</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="s">&#39;Your Web site&#39;</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">comment</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">CommentForm</span><span class="p">(</span><span class="n">auto_id</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Your name:&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;name&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Your Web site:&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;url&quot; name=&quot;url&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Comment:&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;comment&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-initial">
<span id="initial"></span><h3><tt class="docutils literal"><span class="pre">initial</span></tt><a class="headerlink" href="#initial" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.forms.Field.initial">
<tt class="descclassname">Field.</tt><tt class="descname">initial</tt><a class="headerlink" href="#django.forms.Field.initial" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The <tt class="docutils literal"><span class="pre">initial</span></tt> argument lets you specify the initial value to use when
rendering this <tt class="docutils literal"><span class="pre">Field</span></tt> in an unbound <tt class="docutils literal"><span class="pre">Form</span></tt>.</p>
<p>To specify dynamic initial data, see the <a class="reference internal" href="api.html#django.forms.Form.initial" title="django.forms.Form.initial"><tt class="xref py py-attr docutils literal"><span class="pre">Form.initial</span></tt></a> parameter.</p>
<p>The use-case for this is when you want to display an &#8220;empty&#8221; form in which a
field is initialized to a particular value. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">CommentForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">name</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="s">&#39;Your name&#39;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">url</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">URLField</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="s">&#39;http://&#39;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">comment</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">CommentForm</span><span class="p">(</span><span class="n">auto_id</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Name:&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;name&quot; value=&quot;Your name&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Url:&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;url&quot; name=&quot;url&quot; value=&quot;http://&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Comment:&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;comment&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
<p>You may be thinking, why not just pass a dictionary of the initial values as
data when displaying the form? Well, if you do that, you&#8217;ll trigger validation,
and the HTML output will include any validation errors:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">CommentForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">name</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">()</span>
<span class="gp">... </span>    <span class="n">url</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">URLField</span><span class="p">()</span>
<span class="gp">... </span>    <span class="n">comment</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">default_data</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;name&#39;</span><span class="p">:</span> <span class="s">&#39;Your name&#39;</span><span class="p">,</span> <span class="s">&#39;url&#39;</span><span class="p">:</span> <span class="s">&#39;http://&#39;</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">CommentForm</span><span class="p">(</span><span class="n">default_data</span><span class="p">,</span> <span class="n">auto_id</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Name:&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;name&quot; value=&quot;Your name&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Url:&lt;/th&gt;&lt;td&gt;&lt;ul class=&quot;errorlist&quot;&gt;&lt;li&gt;Enter a valid URL.&lt;/li&gt;&lt;/ul&gt;&lt;input type=&quot;url&quot; name=&quot;url&quot; value=&quot;http://&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Comment:&lt;/th&gt;&lt;td&gt;&lt;ul class=&quot;errorlist&quot;&gt;&lt;li&gt;This field is required.&lt;/li&gt;&lt;/ul&gt;&lt;input type=&quot;text&quot; name=&quot;comment&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
<p>This is why <tt class="docutils literal"><span class="pre">initial</span></tt> values are only displayed for unbound forms. For bound
forms, the HTML output will use the bound data.</p>
<p>Also note that <tt class="docutils literal"><span class="pre">initial</span></tt> values are <em>not</em> used as &#8220;fallback&#8221; data in
validation if a particular field&#8217;s value is not given. <tt class="docutils literal"><span class="pre">initial</span></tt> values are
<em>only</em> intended for initial form display:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">CommentForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">name</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="s">&#39;Your name&#39;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">url</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">URLField</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="s">&#39;http://&#39;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">comment</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;name&#39;</span><span class="p">:</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;url&#39;</span><span class="p">:</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;comment&#39;</span><span class="p">:</span> <span class="s">&#39;Foo&#39;</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">CommentForm</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">False</span>
<span class="go"># The form does *not* fall back to using the initial values.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">errors</span>
<span class="go">{&#39;url&#39;: [u&#39;This field is required.&#39;], &#39;name&#39;: [u&#39;This field is required.&#39;]}</span>
</pre></div>
</div>
<p>Instead of a constant, you can also pass any callable:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">datetime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">DateForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">day</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">DateField</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">DateForm</span><span class="p">())</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Day:&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;day&quot; value=&quot;12/23/2008&quot; /&gt;&lt;td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
<p>The callable will be evaluated only when the unbound form is displayed, not when it is defined.</p>
</div>
<div class="section" id="s-widget">
<span id="widget"></span><h3><tt class="docutils literal"><span class="pre">widget</span></tt><a class="headerlink" href="#widget" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.forms.Field.widget">
<tt class="descclassname">Field.</tt><tt class="descname">widget</tt><a class="headerlink" href="#django.forms.Field.widget" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The <tt class="docutils literal"><span class="pre">widget</span></tt> argument lets you specify a <tt class="docutils literal"><span class="pre">Widget</span></tt> class to use when
rendering this <tt class="docutils literal"><span class="pre">Field</span></tt>. See <a class="reference internal" href="widgets.html"><em>Widgets</em></a> for more information.</p>
</div>
<div class="section" id="s-help-text">
<span id="help-text"></span><h3><tt class="docutils literal"><span class="pre">help_text</span></tt><a class="headerlink" href="#help-text" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.forms.Field.help_text">
<tt class="descclassname">Field.</tt><tt class="descname">help_text</tt><a class="headerlink" href="#django.forms.Field.help_text" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The <tt class="docutils literal"><span class="pre">help_text</span></tt> argument lets you specify descriptive text for this
<tt class="docutils literal"><span class="pre">Field</span></tt>. If you provide <tt class="docutils literal"><span class="pre">help_text</span></tt>, it will be displayed next to the
<tt class="docutils literal"><span class="pre">Field</span></tt> when the <tt class="docutils literal"><span class="pre">Field</span></tt> is rendered by one of the convenience <tt class="docutils literal"><span class="pre">Form</span></tt>
methods (e.g., <tt class="docutils literal"><span class="pre">as_ul()</span></tt>).</p>
<p>Here&#8217;s a full example <tt class="docutils literal"><span class="pre">Form</span></tt> that implements <tt class="docutils literal"><span class="pre">help_text</span></tt> for two of its
fields. We&#8217;ve specified <tt class="docutils literal"><span class="pre">auto_id=False</span></tt> to simplify the output:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">HelpTextContactForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">subject</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">help_text</span><span class="o">=</span><span class="s">&#39;100 characters max.&#39;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">message</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">()</span>
<span class="gp">... </span>    <span class="n">sender</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">EmailField</span><span class="p">(</span><span class="n">help_text</span><span class="o">=</span><span class="s">&#39;A valid email address, please.&#39;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">cc_myself</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">BooleanField</span><span class="p">(</span><span class="n">required</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">HelpTextContactForm</span><span class="p">(</span><span class="n">auto_id</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">as_table</span><span class="p">())</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Subject:&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; /&gt;&lt;br /&gt;&lt;span class=&quot;helptext&quot;&gt;100 characters max.&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Message:&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;message&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Sender:&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;email&quot; name=&quot;sender&quot; /&gt;&lt;br /&gt;A valid email address, please.&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Cc myself:&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">as_ul</span><span class="p">()))</span>
<span class="go">&lt;li&gt;Subject: &lt;input type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; /&gt; &lt;span class=&quot;helptext&quot;&gt;100 characters max.&lt;/span&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;Message: &lt;input type=&quot;text&quot; name=&quot;message&quot; /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;Sender: &lt;input type=&quot;email&quot; name=&quot;sender&quot; /&gt; A valid email address, please.&lt;/li&gt;</span>
<span class="go">&lt;li&gt;Cc myself: &lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot; /&gt;&lt;/li&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">as_p</span><span class="p">())</span>
<span class="go">&lt;p&gt;Subject: &lt;input type=&quot;text&quot; name=&quot;subject&quot; maxlength=&quot;100&quot; /&gt; &lt;span class=&quot;helptext&quot;&gt;100 characters max.&lt;/span&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;Message: &lt;input type=&quot;text&quot; name=&quot;message&quot; /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;Sender: &lt;input type=&quot;email&quot; name=&quot;sender&quot; /&gt; A valid email address, please.&lt;/p&gt;</span>
<span class="go">&lt;p&gt;Cc myself: &lt;input type=&quot;checkbox&quot; name=&quot;cc_myself&quot; /&gt;&lt;/p&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-error-messages">
<span id="error-messages"></span><h3><tt class="docutils literal"><span class="pre">error_messages</span></tt><a class="headerlink" href="#error-messages" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.forms.Field.error_messages">
<tt class="descclassname">Field.</tt><tt class="descname">error_messages</tt><a class="headerlink" href="#django.forms.Field.error_messages" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The <tt class="docutils literal"><span class="pre">error_messages</span></tt> argument lets you override the default messages that the
field will raise. Pass in a dictionary with keys matching the error messages you
want to override. For example, here is the default error message:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">generic</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">generic</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">ValidationError</span>: <span class="n">[u&#39;This field is required.&#39;]</span>
</pre></div>
</div>
<p>And here is a custom error message:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">name</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">error_messages</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;required&#39;</span><span class="p">:</span> <span class="s">&#39;Please enter your name&#39;</span><span class="p">})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">name</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">ValidationError</span>: <span class="n">[u&#39;Please enter your name&#39;]</span>
</pre></div>
</div>
<p>In the <a class="reference internal" href="#built-in-field-classes">built-in Field classes</a> section below, each <tt class="docutils literal"><span class="pre">Field</span></tt> defines the
error message keys it uses.</p>
</div>
<div class="section" id="s-validators">
<span id="validators"></span><h3><tt class="docutils literal"><span class="pre">validators</span></tt><a class="headerlink" href="#validators" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.forms.Field.validators">
<tt class="descclassname">Field.</tt><tt class="descname">validators</tt><a class="headerlink" href="#django.forms.Field.validators" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The <tt class="docutils literal"><span class="pre">validators</span></tt> argument lets you provide a list of validation functions
for this field.</p>
<p>See the <a class="reference internal" href="../validators.html"><em>validators documentation</em></a> for more information.</p>
</div>
<div class="section" id="s-localize">
<span id="localize"></span><h3><tt class="docutils literal"><span class="pre">localize</span></tt><a class="headerlink" href="#localize" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.forms.Field.localize">
<tt class="descclassname">Field.</tt><tt class="descname">localize</tt><a class="headerlink" href="#django.forms.Field.localize" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The <tt class="docutils literal"><span class="pre">localize</span></tt> argument enables the localization of form data, input as well
as the rendered output.</p>
<p>See the <a class="reference internal" href="../../topics/i18n/formatting.html#format-localization"><em>format localization</em></a> documentation for
more information.</p>
</div>
</div>
<div class="section" id="s-built-in-field-classes">
<span id="s-built-in-fields"></span><span id="built-in-field-classes"></span><span id="built-in-fields"></span><h2>Built-in <tt class="docutils literal"><span class="pre">Field</span></tt> classes<a class="headerlink" href="#built-in-field-classes" title="Permalink to this headline">¶</a></h2>
<p>Naturally, the <tt class="docutils literal"><span class="pre">forms</span></tt> library comes with a set of <tt class="docutils literal"><span class="pre">Field</span></tt> classes that
represent common validation needs. This section documents each built-in field.</p>
<p>For each field, we describe the default widget used if you don&#8217;t specify
<tt class="docutils literal"><span class="pre">widget</span></tt>. We also specify the value returned when you provide an empty value
(see the section on <tt class="docutils literal"><span class="pre">required</span></tt> above to understand what that means).</p>
<div class="section" id="s-booleanfield">
<span id="booleanfield"></span><h3><tt class="docutils literal"><span class="pre">BooleanField</span></tt><a class="headerlink" href="#booleanfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.BooleanField">
<em class="property">class </em><tt class="descname">BooleanField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.BooleanField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.CheckboxInput" title="django.forms.CheckboxInput"><tt class="xref py py-class docutils literal"><span class="pre">CheckboxInput</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">False</span></tt></li>
<li>Normalizes to: A Python <tt class="docutils literal"><span class="pre">True</span></tt> or <tt class="docutils literal"><span class="pre">False</span></tt> value.</li>
<li>Validates that the value is <tt class="docutils literal"><span class="pre">True</span></tt> (e.g. the check box is checked) if
the field has <tt class="docutils literal"><span class="pre">required=True</span></tt>.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt></li>
</ul>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Since all <tt class="docutils literal"><span class="pre">Field</span></tt> subclasses have <tt class="docutils literal"><span class="pre">required=True</span></tt> by default, the
validation condition here is important. If you want to include a boolean
in your form that can be either <tt class="docutils literal"><span class="pre">True</span></tt> or <tt class="docutils literal"><span class="pre">False</span></tt> (e.g. a checked or
unchecked checkbox), you must remember to pass in <tt class="docutils literal"><span class="pre">required=False</span></tt> when
creating the <tt class="docutils literal"><span class="pre">BooleanField</span></tt>.</p>
</div>
</dd></dl>

</div>
<div class="section" id="s-charfield">
<span id="charfield"></span><h3><tt class="docutils literal"><span class="pre">CharField</span></tt><a class="headerlink" href="#charfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.CharField">
<em class="property">class </em><tt class="descname">CharField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.CharField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">''</span></tt> (an empty string)</li>
<li>Normalizes to: A Unicode object.</li>
<li>Validates <tt class="docutils literal"><span class="pre">max_length</span></tt> or <tt class="docutils literal"><span class="pre">min_length</span></tt>, if they are provided.
Otherwise, all inputs are valid.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">max_length</span></tt>, <tt class="docutils literal"><span class="pre">min_length</span></tt></li>
</ul>
<p>Has two optional arguments for validation:</p>
<dl class="attribute">
<dt id="django.forms.CharField.max_length">
<tt class="descname">max_length</tt><a class="headerlink" href="#django.forms.CharField.max_length" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="attribute">
<dt id="django.forms.CharField.min_length">
<tt class="descname">min_length</tt><a class="headerlink" href="#django.forms.CharField.min_length" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If provided, these arguments ensure that the string is at most or at least
the given length.</p>
</dd></dl>

</div>
<div class="section" id="s-choicefield">
<span id="choicefield"></span><h3><tt class="docutils literal"><span class="pre">ChoiceField</span></tt><a class="headerlink" href="#choicefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.ChoiceField">
<em class="property">class </em><tt class="descname">ChoiceField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.ChoiceField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.Select" title="django.forms.Select"><tt class="xref py py-class docutils literal"><span class="pre">Select</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">''</span></tt> (an empty string)</li>
<li>Normalizes to: A Unicode object.</li>
<li>Validates that the given value exists in the list of choices.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid_choice</span></tt></li>
</ul>
<p>The <tt class="docutils literal"><span class="pre">invalid_choice</span></tt> error message may contain <tt class="docutils literal"><span class="pre">%(value)s</span></tt>, which will be
replaced with the selected choice.</p>
<p>Takes one extra required argument:</p>
<dl class="attribute">
<dt id="django.forms.ChoiceField.choices">
<tt class="descname">choices</tt><a class="headerlink" href="#django.forms.ChoiceField.choices" title="Permalink to this definition">¶</a></dt>
<dd><p>An iterable (e.g., a list or tuple) of 2-tuples to use as choices for this
field. This argument accepts the same formats as the <tt class="docutils literal"><span class="pre">choices</span></tt> argument
to a model field. See the <a class="reference internal" href="../models/fields.html#field-choices"><em>model field reference documentation on
choices</em></a> for more details.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-typedchoicefield">
<span id="typedchoicefield"></span><h3><tt class="docutils literal"><span class="pre">TypedChoiceField</span></tt><a class="headerlink" href="#typedchoicefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.TypedChoiceField">
<em class="property">class </em><tt class="descname">TypedChoiceField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.TypedChoiceField" title="Permalink to this definition">¶</a></dt>
<dd><p>Just like a <a class="reference internal" href="#django.forms.ChoiceField" title="django.forms.ChoiceField"><tt class="xref py py-class docutils literal"><span class="pre">ChoiceField</span></tt></a>, except <a class="reference internal" href="#django.forms.TypedChoiceField" title="django.forms.TypedChoiceField"><tt class="xref py py-class docutils literal"><span class="pre">TypedChoiceField</span></tt></a> takes two
extra arguments, <tt class="docutils literal"><span class="pre">coerce</span></tt> and <tt class="docutils literal"><span class="pre">empty_value</span></tt>.</p>
<ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.Select" title="django.forms.Select"><tt class="xref py py-class docutils literal"><span class="pre">Select</span></tt></a></li>
<li>Empty value: Whatever you&#8217;ve given as <tt class="docutils literal"><span class="pre">empty_value</span></tt></li>
<li>Normalizes to: A value of the type provided by the <tt class="docutils literal"><span class="pre">coerce</span></tt> argument.</li>
<li>Validates that the given value exists in the list of choices and can be
coerced.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid_choice</span></tt></li>
</ul>
<p>Takes extra arguments:</p>
<dl class="attribute">
<dt id="django.forms.TypedChoiceField.coerce">
<tt class="descname">coerce</tt><a class="headerlink" href="#django.forms.TypedChoiceField.coerce" title="Permalink to this definition">¶</a></dt>
<dd><p>A function that takes one argument and returns a coerced value. Examples
include the built-in <tt class="docutils literal"><span class="pre">int</span></tt>, <tt class="docutils literal"><span class="pre">float</span></tt>, <tt class="docutils literal"><span class="pre">bool</span></tt> and other types. Defaults
to an identity function. Note that coercion happens after input
validation, so it is possible to coerce to a value not present in
<tt class="docutils literal"><span class="pre">choices</span></tt>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.forms.TypedChoiceField.empty_value">
<tt class="descname">empty_value</tt><a class="headerlink" href="#django.forms.TypedChoiceField.empty_value" title="Permalink to this definition">¶</a></dt>
<dd><p>The value to use to represent &#8220;empty.&#8221; Defaults to the empty string;
<tt class="docutils literal"><span class="pre">None</span></tt> is another common choice here. Note that this value will not be
coerced by the function given in the <tt class="docutils literal"><span class="pre">coerce</span></tt> argument, so choose it
accordingly.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-datefield">
<span id="datefield"></span><h3><tt class="docutils literal"><span class="pre">DateField</span></tt><a class="headerlink" href="#datefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.DateField">
<em class="property">class </em><tt class="descname">DateField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.DateField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.DateInput" title="django.forms.DateInput"><tt class="xref py py-class docutils literal"><span class="pre">DateInput</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">None</span></tt></li>
<li>Normalizes to: A Python <tt class="docutils literal"><span class="pre">datetime.date</span></tt> object.</li>
<li>Validates that the given value is either a <tt class="docutils literal"><span class="pre">datetime.date</span></tt>,
<tt class="docutils literal"><span class="pre">datetime.datetime</span></tt> or string formatted in a particular date format.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt></li>
</ul>
<p>Takes one optional argument:</p>
<dl class="attribute">
<dt id="django.forms.DateField.input_formats">
<tt class="descname">input_formats</tt><a class="headerlink" href="#django.forms.DateField.input_formats" title="Permalink to this definition">¶</a></dt>
<dd><p>A list of formats used to attempt to convert a string to a valid
<tt class="docutils literal"><span class="pre">datetime.date</span></tt> object.</p>
</dd></dl>

<p>If no <tt class="docutils literal"><span class="pre">input_formats</span></tt> argument is provided, the default input formats are:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">[</span><span class="s">&#39;%Y-%m-</span><span class="si">%d</span><span class="s">&#39;</span><span class="p">,</span>      <span class="c"># &#39;2006-10-25&#39;</span>
<span class="s">&#39;%m/</span><span class="si">%d</span><span class="s">/%Y&#39;</span><span class="p">,</span>       <span class="c"># &#39;10/25/2006&#39;</span>
<span class="s">&#39;%m/</span><span class="si">%d</span><span class="s">/%y&#39;</span><span class="p">]</span>       <span class="c"># &#39;10/25/06&#39;</span>
</pre></div>
</div>
<p>Additionally, if you specify <a class="reference internal" href="../settings.html#std:setting-USE_L10N"><tt class="xref std std-setting docutils literal"><span class="pre">USE_L10N=False</span></tt></a> in your settings, the
following will also be included in the default input formats:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">[</span><span class="s">&#39;%b </span><span class="si">%d</span><span class="s"> %Y&#39;</span><span class="p">,</span>      <span class="c"># &#39;Oct 25 2006&#39;</span>
<span class="s">&#39;%b </span><span class="si">%d</span><span class="s">, %Y&#39;</span><span class="p">,</span>      <span class="c"># &#39;Oct 25, 2006&#39;</span>
<span class="s">&#39;</span><span class="si">%d</span><span class="s"> %b %Y&#39;</span><span class="p">,</span>       <span class="c"># &#39;25 Oct 2006&#39;</span>
<span class="s">&#39;</span><span class="si">%d</span><span class="s"> %b, %Y&#39;</span><span class="p">,</span>      <span class="c"># &#39;25 Oct, 2006&#39;</span>
<span class="s">&#39;%B </span><span class="si">%d</span><span class="s"> %Y&#39;</span><span class="p">,</span>       <span class="c"># &#39;October 25 2006&#39;</span>
<span class="s">&#39;%B </span><span class="si">%d</span><span class="s">, %Y&#39;</span><span class="p">,</span>      <span class="c"># &#39;October 25, 2006&#39;</span>
<span class="s">&#39;</span><span class="si">%d</span><span class="s"> %B %Y&#39;</span><span class="p">,</span>       <span class="c"># &#39;25 October 2006&#39;</span>
<span class="s">&#39;</span><span class="si">%d</span><span class="s"> %B, %Y&#39;</span><span class="p">]</span>      <span class="c"># &#39;25 October, 2006&#39;</span>
</pre></div>
</div>
<p>See also <a class="reference internal" href="../../topics/i18n/formatting.html#format-localization"><em>format localization</em></a>.</p>
</dd></dl>

</div>
<div class="section" id="s-datetimefield">
<span id="datetimefield"></span><h3><tt class="docutils literal"><span class="pre">DateTimeField</span></tt><a class="headerlink" href="#datetimefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.DateTimeField">
<em class="property">class </em><tt class="descname">DateTimeField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.DateTimeField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.DateTimeInput" title="django.forms.DateTimeInput"><tt class="xref py py-class docutils literal"><span class="pre">DateTimeInput</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">None</span></tt></li>
<li>Normalizes to: A Python <tt class="docutils literal"><span class="pre">datetime.datetime</span></tt> object.</li>
<li>Validates that the given value is either a <tt class="docutils literal"><span class="pre">datetime.datetime</span></tt>,
<tt class="docutils literal"><span class="pre">datetime.date</span></tt> or string formatted in a particular datetime format.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt></li>
</ul>
<p>Takes one optional argument:</p>
<dl class="attribute">
<dt id="django.forms.DateTimeField.input_formats">
<tt class="descname">input_formats</tt><a class="headerlink" href="#django.forms.DateTimeField.input_formats" title="Permalink to this definition">¶</a></dt>
<dd><p>A list of formats used to attempt to convert a string to a valid
<tt class="docutils literal"><span class="pre">datetime.datetime</span></tt> object.</p>
</dd></dl>

<p>If no <tt class="docutils literal"><span class="pre">input_formats</span></tt> argument is provided, the default input formats are:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">[</span><span class="s">&#39;%Y-%m-</span><span class="si">%d</span><span class="s"> %H:%M:%S&#39;</span><span class="p">,</span>    <span class="c"># &#39;2006-10-25 14:30:59&#39;</span>
<span class="s">&#39;%Y-%m-</span><span class="si">%d</span><span class="s"> %H:%M&#39;</span><span class="p">,</span>        <span class="c"># &#39;2006-10-25 14:30&#39;</span>
<span class="s">&#39;%Y-%m-</span><span class="si">%d</span><span class="s">&#39;</span><span class="p">,</span>              <span class="c"># &#39;2006-10-25&#39;</span>
<span class="s">&#39;%m/</span><span class="si">%d</span><span class="s">/%Y %H:%M:%S&#39;</span><span class="p">,</span>     <span class="c"># &#39;10/25/2006 14:30:59&#39;</span>
<span class="s">&#39;%m/</span><span class="si">%d</span><span class="s">/%Y %H:%M&#39;</span><span class="p">,</span>        <span class="c"># &#39;10/25/2006 14:30&#39;</span>
<span class="s">&#39;%m/</span><span class="si">%d</span><span class="s">/%Y&#39;</span><span class="p">,</span>              <span class="c"># &#39;10/25/2006&#39;</span>
<span class="s">&#39;%m/</span><span class="si">%d</span><span class="s">/%y %H:%M:%S&#39;</span><span class="p">,</span>     <span class="c"># &#39;10/25/06 14:30:59&#39;</span>
<span class="s">&#39;%m/</span><span class="si">%d</span><span class="s">/%y %H:%M&#39;</span><span class="p">,</span>        <span class="c"># &#39;10/25/06 14:30&#39;</span>
<span class="s">&#39;%m/</span><span class="si">%d</span><span class="s">/%y&#39;</span><span class="p">]</span>              <span class="c"># &#39;10/25/06&#39;</span>
</pre></div>
</div>
<p>See also <a class="reference internal" href="../../topics/i18n/formatting.html#format-localization"><em>format localization</em></a>.</p>
<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 1.7: </span>The ability to use <a class="reference internal" href="widgets.html#django.forms.SplitDateTimeWidget" title="django.forms.SplitDateTimeWidget"><tt class="xref py py-class docutils literal"><span class="pre">SplitDateTimeWidget</span></tt></a> with <tt class="docutils literal"><span class="pre">DateTimeField</span></tt>
has been deprecated and will be removed in Django 1.9. Use
<a class="reference internal" href="#django.forms.SplitDateTimeField" title="django.forms.SplitDateTimeField"><tt class="xref py py-class docutils literal"><span class="pre">SplitDateTimeField</span></tt></a> instead.</p>
</div>
</dd></dl>

</div>
<div class="section" id="s-decimalfield">
<span id="decimalfield"></span><h3><tt class="docutils literal"><span class="pre">DecimalField</span></tt><a class="headerlink" href="#decimalfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.DecimalField">
<em class="property">class </em><tt class="descname">DecimalField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.DecimalField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.NumberInput" title="django.forms.NumberInput"><tt class="xref py py-class docutils literal"><span class="pre">NumberInput</span></tt></a> when <a class="reference internal" href="#django.forms.Field.localize" title="django.forms.Field.localize"><tt class="xref py py-attr docutils literal"><span class="pre">Field.localize</span></tt></a> is
<tt class="docutils literal"><span class="pre">False</span></tt>, else <a class="reference internal" href="widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a>.</li>
<li>Empty value: <tt class="docutils literal"><span class="pre">None</span></tt></li>
<li>Normalizes to: A Python <tt class="docutils literal"><span class="pre">decimal</span></tt>.</li>
<li>Validates that the given value is a decimal. Leading and trailing
whitespace is ignored.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt>, <tt class="docutils literal"><span class="pre">max_value</span></tt>,
<tt class="docutils literal"><span class="pre">min_value</span></tt>, <tt class="docutils literal"><span class="pre">max_digits</span></tt>, <tt class="docutils literal"><span class="pre">max_decimal_places</span></tt>,
<tt class="docutils literal"><span class="pre">max_whole_digits</span></tt></li>
</ul>
<p>The <tt class="docutils literal"><span class="pre">max_value</span></tt> and <tt class="docutils literal"><span class="pre">min_value</span></tt> error messages may contain
<tt class="docutils literal"><span class="pre">%(limit_value)s</span></tt>, which will be substituted by the appropriate limit.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.6:</span> <p>Similarly, the <tt class="docutils literal"><span class="pre">max_digits</span></tt>, <tt class="docutils literal"><span class="pre">max_decimal_places</span></tt> and
<tt class="docutils literal"><span class="pre">max_whole_digits</span></tt> error messages may contain <tt class="docutils literal"><span class="pre">%(max)s</span></tt>.</p>
</div>
<p>Takes four optional arguments:</p>
<dl class="attribute">
<dt id="django.forms.DecimalField.max_value">
<tt class="descname">max_value</tt><a class="headerlink" href="#django.forms.DecimalField.max_value" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="attribute">
<dt id="django.forms.DecimalField.min_value">
<tt class="descname">min_value</tt><a class="headerlink" href="#django.forms.DecimalField.min_value" title="Permalink to this definition">¶</a></dt>
<dd><p>These control the range of values permitted in the field, and should be
given as <tt class="docutils literal"><span class="pre">decimal.Decimal</span></tt> values.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.forms.DecimalField.max_digits">
<tt class="descname">max_digits</tt><a class="headerlink" href="#django.forms.DecimalField.max_digits" title="Permalink to this definition">¶</a></dt>
<dd><p>The maximum number of digits (those before the decimal point plus those
after the decimal point, with leading zeros stripped) permitted in the
value.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.forms.DecimalField.decimal_places">
<tt class="descname">decimal_places</tt><a class="headerlink" href="#django.forms.DecimalField.decimal_places" title="Permalink to this definition">¶</a></dt>
<dd><p>The maximum number of decimal places permitted.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-emailfield">
<span id="emailfield"></span><h3><tt class="docutils literal"><span class="pre">EmailField</span></tt><a class="headerlink" href="#emailfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.EmailField">
<em class="property">class </em><tt class="descname">EmailField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.EmailField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.EmailInput" title="django.forms.EmailInput"><tt class="xref py py-class docutils literal"><span class="pre">EmailInput</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">''</span></tt> (an empty string)</li>
<li>Normalizes to: A Unicode object.</li>
<li>Validates that the given value is a valid email address, using a
moderately complex regular expression.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt></li>
</ul>
<p>Has two optional arguments for validation, <tt class="docutils literal"><span class="pre">max_length</span></tt> and <tt class="docutils literal"><span class="pre">min_length</span></tt>.
If provided, these arguments ensure that the string is at most or at least the
given length.</p>
</dd></dl>

</div>
<div class="section" id="s-filefield">
<span id="filefield"></span><h3><tt class="docutils literal"><span class="pre">FileField</span></tt><a class="headerlink" href="#filefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.FileField">
<em class="property">class </em><tt class="descname">FileField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.FileField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.ClearableFileInput" title="django.forms.ClearableFileInput"><tt class="xref py py-class docutils literal"><span class="pre">ClearableFileInput</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">None</span></tt></li>
<li>Normalizes to: An <tt class="docutils literal"><span class="pre">UploadedFile</span></tt> object that wraps the file content
and file name into a single object.</li>
<li>Can validate that non-empty file data has been bound to the form.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt>, <tt class="docutils literal"><span class="pre">missing</span></tt>, <tt class="docutils literal"><span class="pre">empty</span></tt>,
<tt class="docutils literal"><span class="pre">max_length</span></tt></li>
</ul>
<p>Has two optional arguments for validation, <tt class="docutils literal"><span class="pre">max_length</span></tt> and
<tt class="docutils literal"><span class="pre">allow_empty_file</span></tt>. If provided, these ensure that the file name is at
most the given length, and that validation will succeed even if the file
content is empty.</p>
<p>To learn more about the <tt class="docutils literal"><span class="pre">UploadedFile</span></tt> object, see the <a class="reference internal" href="../../topics/http/file-uploads.html"><em>file uploads
documentation</em></a>.</p>
<p>When you use a <tt class="docutils literal"><span class="pre">FileField</span></tt> in a form, you must also remember to
<a class="reference internal" href="api.html#binding-uploaded-files"><em>bind the file data to the form</em></a>.</p>
<p>The <tt class="docutils literal"><span class="pre">max_length</span></tt> error refers to the length of the filename. In the error
message for that key, <tt class="docutils literal"><span class="pre">%(max)d</span></tt> will be replaced with the maximum filename
length and <tt class="docutils literal"><span class="pre">%(length)d</span></tt> will be replaced with the current filename length.</p>
</dd></dl>

</div>
<div class="section" id="s-filepathfield">
<span id="filepathfield"></span><h3><tt class="docutils literal"><span class="pre">FilePathField</span></tt><a class="headerlink" href="#filepathfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.FilePathField">
<em class="property">class </em><tt class="descname">FilePathField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.FilePathField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.Select" title="django.forms.Select"><tt class="xref py py-class docutils literal"><span class="pre">Select</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">None</span></tt></li>
<li>Normalizes to: A unicode object</li>
<li>Validates that the selected choice exists in the list of choices.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid_choice</span></tt></li>
</ul>
<p>The field allows choosing from files inside a certain directory. It takes three
extra arguments; only <tt class="docutils literal"><span class="pre">path</span></tt> is required:</p>
<dl class="attribute">
<dt id="django.forms.FilePathField.path">
<tt class="descname">path</tt><a class="headerlink" href="#django.forms.FilePathField.path" title="Permalink to this definition">¶</a></dt>
<dd><p>The absolute path to the directory whose contents you want listed. This
directory must exist.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.forms.FilePathField.recursive">
<tt class="descname">recursive</tt><a class="headerlink" href="#django.forms.FilePathField.recursive" title="Permalink to this definition">¶</a></dt>
<dd><p>If <tt class="docutils literal"><span class="pre">False</span></tt> (the default) only the direct contents of <tt class="docutils literal"><span class="pre">path</span></tt> will be
offered as choices. If <tt class="docutils literal"><span class="pre">True</span></tt>, the directory will be descended into
recursively and all descendants will be listed as choices.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.forms.FilePathField.match">
<tt class="descname">match</tt><a class="headerlink" href="#django.forms.FilePathField.match" title="Permalink to this definition">¶</a></dt>
<dd><p>A regular expression pattern; only files with names matching this expression
will be allowed as choices.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.forms.FilePathField.allow_files">
<tt class="descname">allow_files</tt><a class="headerlink" href="#django.forms.FilePathField.allow_files" title="Permalink to this definition">¶</a></dt>
<dd><p>Optional.  Either <tt class="docutils literal"><span class="pre">True</span></tt> or <tt class="docutils literal"><span class="pre">False</span></tt>.  Default is <tt class="docutils literal"><span class="pre">True</span></tt>.  Specifies
whether files in the specified location should be included.  Either this or
<a class="reference internal" href="#django.forms.FilePathField.allow_folders" title="django.forms.FilePathField.allow_folders"><tt class="xref py py-attr docutils literal"><span class="pre">allow_folders</span></tt></a> must be <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.forms.FilePathField.allow_folders">
<tt class="descname">allow_folders</tt><a class="headerlink" href="#django.forms.FilePathField.allow_folders" title="Permalink to this definition">¶</a></dt>
<dd><p>Optional.  Either <tt class="docutils literal"><span class="pre">True</span></tt> or <tt class="docutils literal"><span class="pre">False</span></tt>.  Default is <tt class="docutils literal"><span class="pre">False</span></tt>.  Specifies
whether folders in the specified location should be included.  Either this or
<a class="reference internal" href="#django.forms.FilePathField.allow_files" title="django.forms.FilePathField.allow_files"><tt class="xref py py-attr docutils literal"><span class="pre">allow_files</span></tt></a> must be <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-floatfield">
<span id="floatfield"></span><h3><tt class="docutils literal"><span class="pre">FloatField</span></tt><a class="headerlink" href="#floatfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.FloatField">
<em class="property">class </em><tt class="descname">FloatField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.FloatField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.NumberInput" title="django.forms.NumberInput"><tt class="xref py py-class docutils literal"><span class="pre">NumberInput</span></tt></a> when <a class="reference internal" href="#django.forms.Field.localize" title="django.forms.Field.localize"><tt class="xref py py-attr docutils literal"><span class="pre">Field.localize</span></tt></a> is
<tt class="docutils literal"><span class="pre">False</span></tt>, else <a class="reference internal" href="widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a>.</li>
<li>Empty value: <tt class="docutils literal"><span class="pre">None</span></tt></li>
<li>Normalizes to: A Python float.</li>
<li>Validates that the given value is an float. Leading and trailing
whitespace is allowed, as in Python&#8217;s <tt class="docutils literal"><span class="pre">float()</span></tt> function.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt>, <tt class="docutils literal"><span class="pre">max_value</span></tt>,
<tt class="docutils literal"><span class="pre">min_value</span></tt></li>
</ul>
<p>Takes two optional arguments for validation, <tt class="docutils literal"><span class="pre">max_value</span></tt> and <tt class="docutils literal"><span class="pre">min_value</span></tt>.
These control the range of values permitted in the field.</p>
</dd></dl>

</div>
<div class="section" id="s-imagefield">
<span id="imagefield"></span><h3><tt class="docutils literal"><span class="pre">ImageField</span></tt><a class="headerlink" href="#imagefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.ImageField">
<em class="property">class </em><tt class="descname">ImageField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.ImageField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.ClearableFileInput" title="django.forms.ClearableFileInput"><tt class="xref py py-class docutils literal"><span class="pre">ClearableFileInput</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">None</span></tt></li>
<li>Normalizes to: An <tt class="docutils literal"><span class="pre">UploadedFile</span></tt> object that wraps the file content
and file name into a single object.</li>
<li>Validates that file data has been bound to the form, and that the
file is of an image format understood by Pillow/PIL.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt>, <tt class="docutils literal"><span class="pre">missing</span></tt>, <tt class="docutils literal"><span class="pre">empty</span></tt>,
<tt class="docutils literal"><span class="pre">invalid_image</span></tt></li>
</ul>
<p>Using an <tt class="docutils literal"><span class="pre">ImageField</span></tt> requires that either <a class="reference external" href="http://pillow.readthedocs.org/en/latest/">Pillow</a> (recommended) or the
<a class="reference external" href="http://www.pythonware.com/products/pil/">Python Imaging Library</a> (PIL) are installed and supports the image
formats you use. If you encounter a <tt class="docutils literal"><span class="pre">corrupt</span> <span class="pre">image</span></tt> error when you
upload an image, it usually means either Pillow or PIL
doesn&#8217;t understand its format. To fix this, install the appropriate
library and reinstall Pillow or PIL.</p>
<p>When you use an <tt class="docutils literal"><span class="pre">ImageField</span></tt> on a form, you must also remember to
<a class="reference internal" href="api.html#binding-uploaded-files"><em>bind the file data to the form</em></a>.</p>
</dd></dl>

</div>
<div class="section" id="s-integerfield">
<span id="integerfield"></span><h3><tt class="docutils literal"><span class="pre">IntegerField</span></tt><a class="headerlink" href="#integerfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.IntegerField">
<em class="property">class </em><tt class="descname">IntegerField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.IntegerField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.NumberInput" title="django.forms.NumberInput"><tt class="xref py py-class docutils literal"><span class="pre">NumberInput</span></tt></a> when <a class="reference internal" href="#django.forms.Field.localize" title="django.forms.Field.localize"><tt class="xref py py-attr docutils literal"><span class="pre">Field.localize</span></tt></a> is
<tt class="docutils literal"><span class="pre">False</span></tt>, else <a class="reference internal" href="widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a>.</li>
<li>Empty value: <tt class="docutils literal"><span class="pre">None</span></tt></li>
<li>Normalizes to: A Python integer or long integer.</li>
<li>Validates that the given value is an integer. Leading and trailing
whitespace is allowed, as in Python&#8217;s <tt class="docutils literal"><span class="pre">int()</span></tt> function.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt>, <tt class="docutils literal"><span class="pre">max_value</span></tt>,
<tt class="docutils literal"><span class="pre">min_value</span></tt></li>
</ul>
<p>The <tt class="docutils literal"><span class="pre">max_value</span></tt> and <tt class="docutils literal"><span class="pre">min_value</span></tt> error messages may contain
<tt class="docutils literal"><span class="pre">%(limit_value)s</span></tt>, which will be substituted by the appropriate limit.</p>
<p>Takes two optional arguments for validation:</p>
<dl class="attribute">
<dt id="django.forms.IntegerField.max_value">
<tt class="descname">max_value</tt><a class="headerlink" href="#django.forms.IntegerField.max_value" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="attribute">
<dt id="django.forms.IntegerField.min_value">
<tt class="descname">min_value</tt><a class="headerlink" href="#django.forms.IntegerField.min_value" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>These control the range of values permitted in the field.</p>
</dd></dl>

</div>
<div class="section" id="s-ipaddressfield">
<span id="ipaddressfield"></span><h3><tt class="docutils literal"><span class="pre">IPAddressField</span></tt><a class="headerlink" href="#ipaddressfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.IPAddressField">
<em class="property">class </em><tt class="descname">IPAddressField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.IPAddressField" title="Permalink to this definition">¶</a></dt>
<dd><div class="deprecated">
<p><span class="versionmodified">Deprecated since version 1.7: </span>This field has been deprecated in favor of
<a class="reference internal" href="#django.forms.GenericIPAddressField" title="django.forms.GenericIPAddressField"><tt class="xref py py-class docutils literal"><span class="pre">GenericIPAddressField</span></tt></a>.</p>
</div>
<ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">''</span></tt> (an empty string)</li>
<li>Normalizes to: A Unicode object.</li>
<li>Validates that the given value is a valid IPv4 address, using a regular
expression.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt></li>
</ul>
</dd></dl>

</div>
<div class="section" id="s-genericipaddressfield">
<span id="genericipaddressfield"></span><h3><tt class="docutils literal"><span class="pre">GenericIPAddressField</span></tt><a class="headerlink" href="#genericipaddressfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.GenericIPAddressField">
<em class="property">class </em><tt class="descname">GenericIPAddressField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.GenericIPAddressField" title="Permalink to this definition">¶</a></dt>
<dd><p>A field containing either an IPv4 or an IPv6 address.</p>
<ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">''</span></tt> (an empty string)</li>
<li>Normalizes to: A Unicode object. IPv6 addresses are
normalized as described below.</li>
<li>Validates that the given value is a valid IP address.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt></li>
</ul>
<p>The IPv6 address normalization follows <span class="target" id="index-0"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc4291.html#section-2.2"><strong>RFC 4291</strong></a> section 2.2,
including using the IPv4 format suggested in paragraph 3 of that section, like
<tt class="docutils literal"><span class="pre">::ffff:192.0.2.0</span></tt>. For example, <tt class="docutils literal"><span class="pre">2001:0::0:01</span></tt> would be normalized to
<tt class="docutils literal"><span class="pre">2001::1</span></tt>, and <tt class="docutils literal"><span class="pre">::ffff:0a0a:0a0a</span></tt> to <tt class="docutils literal"><span class="pre">::ffff:10.10.10.10</span></tt>. All characters
are converted to lowercase.</p>
<p>Takes two optional arguments:</p>
<dl class="attribute">
<dt id="django.forms.GenericIPAddressField.protocol">
<tt class="descname">protocol</tt><a class="headerlink" href="#django.forms.GenericIPAddressField.protocol" title="Permalink to this definition">¶</a></dt>
<dd><p>Limits valid inputs to the specified protocol.
Accepted values are <tt class="docutils literal"><span class="pre">both</span></tt> (default), <tt class="docutils literal"><span class="pre">IPv4</span></tt>
or <tt class="docutils literal"><span class="pre">IPv6</span></tt>. Matching is case insensitive.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.forms.GenericIPAddressField.unpack_ipv4">
<tt class="descname">unpack_ipv4</tt><a class="headerlink" href="#django.forms.GenericIPAddressField.unpack_ipv4" title="Permalink to this definition">¶</a></dt>
<dd><p>Unpacks IPv4 mapped addresses like <tt class="docutils literal"><span class="pre">::ffff:192.0.2.1</span></tt>.
If this option is enabled that address would be unpacked to
<tt class="docutils literal"><span class="pre">192.0.2.1</span></tt>. Default is disabled. Can only be used
when <tt class="docutils literal"><span class="pre">protocol</span></tt> is set to <tt class="docutils literal"><span class="pre">'both'</span></tt>.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-multiplechoicefield">
<span id="multiplechoicefield"></span><h3><tt class="docutils literal"><span class="pre">MultipleChoiceField</span></tt><a class="headerlink" href="#multiplechoicefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.MultipleChoiceField">
<em class="property">class </em><tt class="descname">MultipleChoiceField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.MultipleChoiceField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.SelectMultiple" title="django.forms.SelectMultiple"><tt class="xref py py-class docutils literal"><span class="pre">SelectMultiple</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">[]</span></tt> (an empty list)</li>
<li>Normalizes to: A list of Unicode objects.</li>
<li>Validates that every value in the given list of values exists in the list
of choices.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid_choice</span></tt>, <tt class="docutils literal"><span class="pre">invalid_list</span></tt></li>
</ul>
<p>The <tt class="docutils literal"><span class="pre">invalid_choice</span></tt> error message may contain <tt class="docutils literal"><span class="pre">%(value)s</span></tt>, which will be
replaced with the selected choice.</p>
<p>Takes one extra required argument, <tt class="docutils literal"><span class="pre">choices</span></tt>, as for <tt class="docutils literal"><span class="pre">ChoiceField</span></tt>.</p>
</dd></dl>

</div>
<div class="section" id="s-typedmultiplechoicefield">
<span id="typedmultiplechoicefield"></span><h3><tt class="docutils literal"><span class="pre">TypedMultipleChoiceField</span></tt><a class="headerlink" href="#typedmultiplechoicefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.TypedMultipleChoiceField">
<em class="property">class </em><tt class="descname">TypedMultipleChoiceField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.TypedMultipleChoiceField" title="Permalink to this definition">¶</a></dt>
<dd><p>Just like a <a class="reference internal" href="#django.forms.MultipleChoiceField" title="django.forms.MultipleChoiceField"><tt class="xref py py-class docutils literal"><span class="pre">MultipleChoiceField</span></tt></a>, except <a class="reference internal" href="#django.forms.TypedMultipleChoiceField" title="django.forms.TypedMultipleChoiceField"><tt class="xref py py-class docutils literal"><span class="pre">TypedMultipleChoiceField</span></tt></a>
takes two extra arguments, <tt class="docutils literal"><span class="pre">coerce</span></tt> and <tt class="docutils literal"><span class="pre">empty_value</span></tt>.</p>
<ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.SelectMultiple" title="django.forms.SelectMultiple"><tt class="xref py py-class docutils literal"><span class="pre">SelectMultiple</span></tt></a></li>
<li>Empty value: Whatever you&#8217;ve given as <tt class="docutils literal"><span class="pre">empty_value</span></tt></li>
<li>Normalizes to: A list of values of the type provided by the <tt class="docutils literal"><span class="pre">coerce</span></tt>
argument.</li>
<li>Validates that the given values exists in the list of choices and can be
coerced.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid_choice</span></tt></li>
</ul>
<p>The <tt class="docutils literal"><span class="pre">invalid_choice</span></tt> error message may contain <tt class="docutils literal"><span class="pre">%(value)s</span></tt>, which will be
replaced with the selected choice.</p>
<p>Takes two extra arguments, <tt class="docutils literal"><span class="pre">coerce</span></tt> and <tt class="docutils literal"><span class="pre">empty_value</span></tt>, as for <tt class="docutils literal"><span class="pre">TypedChoiceField</span></tt>.</p>
</dd></dl>

</div>
<div class="section" id="s-nullbooleanfield">
<span id="nullbooleanfield"></span><h3><tt class="docutils literal"><span class="pre">NullBooleanField</span></tt><a class="headerlink" href="#nullbooleanfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.NullBooleanField">
<em class="property">class </em><tt class="descname">NullBooleanField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.NullBooleanField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.NullBooleanSelect" title="django.forms.NullBooleanSelect"><tt class="xref py py-class docutils literal"><span class="pre">NullBooleanSelect</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">None</span></tt></li>
<li>Normalizes to: A Python <tt class="docutils literal"><span class="pre">True</span></tt>, <tt class="docutils literal"><span class="pre">False</span></tt> or <tt class="docutils literal"><span class="pre">None</span></tt> value.</li>
<li>Validates nothing (i.e., it never raises a <tt class="docutils literal"><span class="pre">ValidationError</span></tt>).</li>
</ul>
</dd></dl>

</div>
<div class="section" id="s-regexfield">
<span id="regexfield"></span><h3><tt class="docutils literal"><span class="pre">RegexField</span></tt><a class="headerlink" href="#regexfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.RegexField">
<em class="property">class </em><tt class="descname">RegexField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.RegexField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">''</span></tt> (an empty string)</li>
<li>Normalizes to: A Unicode object.</li>
<li>Validates that the given value matches against a certain regular
expression.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt></li>
</ul>
<p>Takes one required argument:</p>
<dl class="attribute">
<dt id="django.forms.RegexField.regex">
<tt class="descname">regex</tt><a class="headerlink" href="#django.forms.RegexField.regex" title="Permalink to this definition">¶</a></dt>
<dd><p>A regular expression specified either as a string or a compiled regular
expression object.</p>
</dd></dl>

<p>Also takes <tt class="docutils literal"><span class="pre">max_length</span></tt> and <tt class="docutils literal"><span class="pre">min_length</span></tt>, which work just as they do for
<tt class="docutils literal"><span class="pre">CharField</span></tt>.</p>
<p>The optional argument <tt class="docutils literal"><span class="pre">error_message</span></tt> is also accepted for backwards
compatibility. The preferred way to provide an error message is to use the
<tt class="docutils literal"><span class="pre">error_messages</span></tt> argument, passing a dictionary with <tt class="docutils literal"><span class="pre">'invalid'</span></tt> as a key
and the error message as the value.</p>
</dd></dl>

</div>
<div class="section" id="s-slugfield">
<span id="slugfield"></span><h3><tt class="docutils literal"><span class="pre">SlugField</span></tt><a class="headerlink" href="#slugfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.SlugField">
<em class="property">class </em><tt class="descname">SlugField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.SlugField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">''</span></tt> (an empty string)</li>
<li>Normalizes to: A Unicode object.</li>
<li>Validates that the given value contains only letters, numbers,
underscores, and hyphens.</li>
<li>Error messages: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt></li>
</ul>
<p>This field is intended for use in representing a model
<a class="reference internal" href="../models/fields.html#django.db.models.SlugField" title="django.db.models.SlugField"><tt class="xref py py-class docutils literal"><span class="pre">SlugField</span></tt></a> in forms.</p>
</dd></dl>

</div>
<div class="section" id="s-timefield">
<span id="timefield"></span><h3><tt class="docutils literal"><span class="pre">TimeField</span></tt><a class="headerlink" href="#timefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.TimeField">
<em class="property">class </em><tt class="descname">TimeField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.TimeField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">None</span></tt></li>
<li>Normalizes to: A Python <tt class="docutils literal"><span class="pre">datetime.time</span></tt> object.</li>
<li>Validates that the given value is either a <tt class="docutils literal"><span class="pre">datetime.time</span></tt> or string
formatted in a particular time format.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt></li>
</ul>
<p>Takes one optional argument:</p>
<dl class="attribute">
<dt id="django.forms.TimeField.input_formats">
<tt class="descname">input_formats</tt><a class="headerlink" href="#django.forms.TimeField.input_formats" title="Permalink to this definition">¶</a></dt>
<dd><p>A list of formats used to attempt to convert a string to a valid
<tt class="docutils literal"><span class="pre">datetime.time</span></tt> object.</p>
</dd></dl>

<p>If no <tt class="docutils literal"><span class="pre">input_formats</span></tt> argument is provided, the default input formats are:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="s">&#39;%H:%M:%S&#39;</span><span class="p">,</span>     <span class="c"># &#39;14:30:59&#39;</span>
<span class="s">&#39;%H:%M&#39;</span><span class="p">,</span>        <span class="c"># &#39;14:30&#39;</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-urlfield">
<span id="urlfield"></span><h3><tt class="docutils literal"><span class="pre">URLField</span></tt><a class="headerlink" href="#urlfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.URLField">
<em class="property">class </em><tt class="descname">URLField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.URLField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.URLInput" title="django.forms.URLInput"><tt class="xref py py-class docutils literal"><span class="pre">URLInput</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">''</span></tt> (an empty string)</li>
<li>Normalizes to: A Unicode object.</li>
<li>Validates that the given value is a valid URL.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt></li>
</ul>
<p>Takes the following optional arguments:</p>
<dl class="attribute">
<dt id="django.forms.URLField.max_length">
<tt class="descname">max_length</tt><a class="headerlink" href="#django.forms.URLField.max_length" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="attribute">
<dt id="django.forms.URLField.min_length">
<tt class="descname">min_length</tt><a class="headerlink" href="#django.forms.URLField.min_length" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>These are the same as <tt class="docutils literal"><span class="pre">CharField.max_length</span></tt> and <tt class="docutils literal"><span class="pre">CharField.min_length</span></tt>.</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-slightly-complex-built-in-field-classes">
<span id="slightly-complex-built-in-field-classes"></span><h2>Slightly complex built-in <tt class="docutils literal"><span class="pre">Field</span></tt> classes<a class="headerlink" href="#slightly-complex-built-in-field-classes" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-combofield">
<span id="combofield"></span><h3><tt class="docutils literal"><span class="pre">ComboField</span></tt><a class="headerlink" href="#combofield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.ComboField">
<em class="property">class </em><tt class="descname">ComboField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.ComboField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">''</span></tt> (an empty string)</li>
<li>Normalizes to: A Unicode object.</li>
<li>Validates that the given value against each of the fields specified
as an argument to the <tt class="docutils literal"><span class="pre">ComboField</span></tt>.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt></li>
</ul>
<p>Takes one extra required argument:</p>
<dl class="attribute">
<dt id="django.forms.ComboField.fields">
<tt class="descname">fields</tt><a class="headerlink" href="#django.forms.ComboField.fields" title="Permalink to this definition">¶</a></dt>
<dd><p>The list of fields that should be used to validate the field&#8217;s value (in
the order in which they are provided).</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.forms</span> <span class="kn">import</span> <span class="n">ComboField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ComboField</span><span class="p">(</span><span class="n">fields</span><span class="o">=</span><span class="p">[</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">20</span><span class="p">),</span> <span class="n">EmailField</span><span class="p">()])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="s">&#39;test@example.com&#39;</span><span class="p">)</span>
<span class="go">u&#39;test@example.com&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="s">&#39;longemailaddress@example.com&#39;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">ValidationError</span>: <span class="n">[u&#39;Ensure this value has at most 20 characters (it has 28).&#39;]</span>
</pre></div>
</div>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-multivaluefield">
<span id="multivaluefield"></span><h3><tt class="docutils literal"><span class="pre">MultiValueField</span></tt><a class="headerlink" href="#multivaluefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.MultiValueField">
<em class="property">class </em><tt class="descname">MultiValueField</tt>(<em>fields=()</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.forms.MultiValueField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">''</span></tt> (an empty string)</li>
<li>Normalizes to: the type returned by the <tt class="docutils literal"><span class="pre">compress</span></tt> method of the subclass.</li>
<li>Validates that the given value against each of the fields specified
as an argument to the <tt class="docutils literal"><span class="pre">MultiValueField</span></tt>.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt>, <tt class="docutils literal"><span class="pre">incomplete</span></tt></li>
</ul>
<p>Aggregates the logic of multiple fields that together produce a single
value.</p>
<p>This field is abstract and must be subclassed. In contrast with the
single-value fields, subclasses of <a class="reference internal" href="#django.forms.MultiValueField" title="django.forms.MultiValueField"><tt class="xref py py-class docutils literal"><span class="pre">MultiValueField</span></tt></a> must not
implement <a class="reference internal" href="#django.forms.Field.clean" title="django.forms.Field.clean"><tt class="xref py py-meth docutils literal"><span class="pre">clean()</span></tt></a> but instead - implement
<a class="reference internal" href="#django.forms.MultiValueField.compress" title="django.forms.MultiValueField.compress"><tt class="xref py py-meth docutils literal"><span class="pre">compress()</span></tt></a>.</p>
<p>Takes one extra required argument:</p>
<dl class="attribute">
<dt id="django.forms.MultiValueField.fields">
<tt class="descname">fields</tt><a class="headerlink" href="#django.forms.MultiValueField.fields" title="Permalink to this definition">¶</a></dt>
<dd><p>A tuple of fields whose values are cleaned and subsequently combined
into a single value.  Each value of the field is cleaned by the
corresponding field in <tt class="docutils literal"><span class="pre">fields</span></tt> &#8211; the first value is cleaned by the
first field, the second value is cleaned by the second field, etc.
Once all fields are cleaned, the list of clean values is combined into
a single value by <a class="reference internal" href="#django.forms.MultiValueField.compress" title="django.forms.MultiValueField.compress"><tt class="xref py py-meth docutils literal"><span class="pre">compress()</span></tt></a>.</p>
</dd></dl>

<p>Also takes one extra optional argument:</p>
<dl class="attribute">
<dt id="django.forms.MultiValueField.require_all_fields">
<tt class="descname">require_all_fields</tt><a class="headerlink" href="#django.forms.MultiValueField.require_all_fields" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>Defaults to <tt class="docutils literal"><span class="pre">True</span></tt>, in which case a <tt class="docutils literal"><span class="pre">required</span></tt> validation error
will be raised if no value is supplied for any field.</p>
<p>When set to <tt class="docutils literal"><span class="pre">False</span></tt>, the <a class="reference internal" href="#django.forms.Field.required" title="django.forms.Field.required"><tt class="xref py py-attr docutils literal"><span class="pre">Field.required</span></tt></a> attribute can be set
to <tt class="docutils literal"><span class="pre">False</span></tt> for individual fields to make them optional. If no value
is supplied for a required field, an <tt class="docutils literal"><span class="pre">incomplete</span></tt> validation error
will be raised.</p>
<p>A default <tt class="docutils literal"><span class="pre">incomplete</span></tt> error message can be defined on the
<a class="reference internal" href="#django.forms.MultiValueField" title="django.forms.MultiValueField"><tt class="xref py py-class docutils literal"><span class="pre">MultiValueField</span></tt></a> subclass, or different messages can be defined
on each individual field. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.core.validators</span> <span class="kn">import</span> <span class="n">RegexValidator</span>

<span class="k">class</span> <span class="nc">PhoneField</span><span class="p">(</span><span class="n">MultiValueField</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="c"># Define one message for all fields.</span>
        <span class="n">error_messages</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s">&#39;incomplete&#39;</span><span class="p">:</span> <span class="s">&#39;Enter a country calling code and a phone number.&#39;</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="c"># Or define a different message for each field.</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">CharField</span><span class="p">(</span><span class="n">error_messages</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;incomplete&#39;</span><span class="p">:</span> <span class="s">&#39;Enter a country calling code.&#39;</span><span class="p">},</span>
                      <span class="n">validators</span><span class="o">=</span><span class="p">[</span><span class="n">RegexValidator</span><span class="p">(</span><span class="s">r&#39;^\d+$&#39;</span><span class="p">,</span> <span class="s">&#39;Enter a valid country calling code.&#39;</span><span class="p">)]),</span>
            <span class="n">CharField</span><span class="p">(</span><span class="n">error_messages</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;incomplete&#39;</span><span class="p">:</span> <span class="s">&#39;Enter a phone number.&#39;</span><span class="p">},</span>
                      <span class="n">validators</span><span class="o">=</span><span class="p">[</span><span class="n">RegexValidator</span><span class="p">(</span><span class="s">r&#39;^\d+$&#39;</span><span class="p">,</span> <span class="s">&#39;Enter a valid phone number.&#39;</span><span class="p">)]),</span>
            <span class="n">CharField</span><span class="p">(</span><span class="n">validators</span><span class="o">=</span><span class="p">[</span><span class="n">RegexValidator</span><span class="p">(</span><span class="s">r&#39;^\d+$&#39;</span><span class="p">,</span> <span class="s">&#39;Enter a valid extension.&#39;</span><span class="p">)],</span>
                      <span class="n">required</span><span class="o">=</span><span class="bp">False</span><span class="p">),</span>
        <span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">PhoneField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span>
            <span class="n">error_messages</span><span class="o">=</span><span class="n">error_messages</span><span class="p">,</span> <span class="n">fields</span><span class="o">=</span><span class="n">fields</span><span class="p">,</span>
            <span class="n">require_all_fields</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.forms.MultiValueField.widget">
<tt class="descname">widget</tt><a class="headerlink" href="#django.forms.MultiValueField.widget" title="Permalink to this definition">¶</a></dt>
<dd><p>Must be a subclass of <a class="reference internal" href="widgets.html#django.forms.MultiWidget" title="django.forms.MultiWidget"><tt class="xref py py-class docutils literal"><span class="pre">django.forms.MultiWidget</span></tt></a>.
Default value is <a class="reference internal" href="widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a>, which
probably is not very useful in this case.</p>
</dd></dl>

<dl class="method">
<dt id="django.forms.MultiValueField.compress">
<tt class="descname">compress</tt>(<em>data_list</em>)<a class="headerlink" href="#django.forms.MultiValueField.compress" title="Permalink to this definition">¶</a></dt>
<dd><p>Takes a list of valid values and returns  a &#8220;compressed&#8221; version of
those values &#8211; in a single value. For example,
<a class="reference internal" href="#django.forms.SplitDateTimeField" title="django.forms.SplitDateTimeField"><tt class="xref py py-class docutils literal"><span class="pre">SplitDateTimeField</span></tt></a> is a subclass which combines a time field
and a date field into a <tt class="docutils literal"><span class="pre">datetime</span></tt> object.</p>
<p>This method must be implemented in the subclasses.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-splitdatetimefield">
<span id="splitdatetimefield"></span><h3><tt class="docutils literal"><span class="pre">SplitDateTimeField</span></tt><a class="headerlink" href="#splitdatetimefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.SplitDateTimeField">
<em class="property">class </em><tt class="descname">SplitDateTimeField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.SplitDateTimeField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.SplitDateTimeWidget" title="django.forms.SplitDateTimeWidget"><tt class="xref py py-class docutils literal"><span class="pre">SplitDateTimeWidget</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">None</span></tt></li>
<li>Normalizes to: A Python <tt class="docutils literal"><span class="pre">datetime.datetime</span></tt> object.</li>
<li>Validates that the given value is a <tt class="docutils literal"><span class="pre">datetime.datetime</span></tt> or string
formatted in a particular datetime format.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt>, <tt class="docutils literal"><span class="pre">invalid_date</span></tt>,
<tt class="docutils literal"><span class="pre">invalid_time</span></tt></li>
</ul>
<p>Takes two optional arguments:</p>
<dl class="attribute">
<dt id="django.forms.SplitDateTimeField.input_date_formats">
<tt class="descname">input_date_formats</tt><a class="headerlink" href="#django.forms.SplitDateTimeField.input_date_formats" title="Permalink to this definition">¶</a></dt>
<dd><p>A list of formats used to attempt to convert a string to a valid
<tt class="docutils literal"><span class="pre">datetime.date</span></tt> object.</p>
</dd></dl>

<p>If no <tt class="docutils literal"><span class="pre">input_date_formats</span></tt> argument is provided, the default input formats
for <tt class="docutils literal"><span class="pre">DateField</span></tt> are used.</p>
<dl class="attribute">
<dt id="django.forms.SplitDateTimeField.input_time_formats">
<tt class="descname">input_time_formats</tt><a class="headerlink" href="#django.forms.SplitDateTimeField.input_time_formats" title="Permalink to this definition">¶</a></dt>
<dd><p>A list of formats used to attempt to convert a string to a valid
<tt class="docutils literal"><span class="pre">datetime.time</span></tt> object.</p>
</dd></dl>

<p>If no <tt class="docutils literal"><span class="pre">input_time_formats</span></tt> argument is provided, the default input formats
for <tt class="docutils literal"><span class="pre">TimeField</span></tt> are used.</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-fields-which-handle-relationships">
<span id="fields-which-handle-relationships"></span><h2>Fields which handle relationships<a class="headerlink" href="#fields-which-handle-relationships" title="Permalink to this headline">¶</a></h2>
<p>Two fields are available for representing relationships between
models: <a class="reference internal" href="#django.forms.ModelChoiceField" title="django.forms.ModelChoiceField"><tt class="xref py py-class docutils literal"><span class="pre">ModelChoiceField</span></tt></a> and
<a class="reference internal" href="#django.forms.ModelMultipleChoiceField" title="django.forms.ModelMultipleChoiceField"><tt class="xref py py-class docutils literal"><span class="pre">ModelMultipleChoiceField</span></tt></a>.  Both of these fields require a
single <tt class="docutils literal"><span class="pre">queryset</span></tt> parameter that is used to create the choices for
the field.  Upon form validation, these fields will place either one
model object (in the case of <tt class="docutils literal"><span class="pre">ModelChoiceField</span></tt>) or multiple model
objects (in the case of <tt class="docutils literal"><span class="pre">ModelMultipleChoiceField</span></tt>) into the
<tt class="docutils literal"><span class="pre">cleaned_data</span></tt> dictionary of the form.</p>
<p>For more complex uses, you can specify <tt class="docutils literal"><span class="pre">queryset=None</span></tt> when declaring the
form field and then populate the <tt class="docutils literal"><span class="pre">queryset</span></tt> in the form&#8217;s <tt class="docutils literal"><span class="pre">__init__()</span></tt>
method:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">FooMultipleChoiceForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
    <span class="n">foo_select</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">ModelMultipleChoiceField</span><span class="p">(</span><span class="n">queryset</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">FooMultipleChoiceForm</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">[</span><span class="s">&#39;foo_select&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">queryset</span> <span class="o">=</span> <span class="o">...</span>
</pre></div>
</div>
<div class="section" id="s-modelchoicefield">
<span id="modelchoicefield"></span><h3><tt class="docutils literal"><span class="pre">ModelChoiceField</span></tt><a class="headerlink" href="#modelchoicefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.ModelChoiceField">
<em class="property">class </em><tt class="descname">ModelChoiceField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.ModelChoiceField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.Select" title="django.forms.Select"><tt class="xref py py-class docutils literal"><span class="pre">Select</span></tt></a></li>
<li>Empty value: <tt class="docutils literal"><span class="pre">None</span></tt></li>
<li>Normalizes to: A model instance.</li>
<li>Validates that the given id exists in the queryset.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">invalid_choice</span></tt></li>
</ul>
<p>Allows the selection of a single model object, suitable for representing a
foreign key. Note that the default widget for <tt class="docutils literal"><span class="pre">ModelChoiceField</span></tt> becomes
impractical when the number of entries increases. You should avoid using it
for more than 100 items.</p>
<p>A single argument is required:</p>
<dl class="attribute">
<dt id="django.forms.ModelChoiceField.queryset">
<tt class="descname">queryset</tt><a class="headerlink" href="#django.forms.ModelChoiceField.queryset" title="Permalink to this definition">¶</a></dt>
<dd><p>A <tt class="docutils literal"><span class="pre">QuerySet</span></tt> of model objects from which the choices for the
field will be derived, and which will be used to validate the
user&#8217;s selection.</p>
</dd></dl>

<p><tt class="docutils literal"><span class="pre">ModelChoiceField</span></tt> also takes two optional arguments:</p>
<dl class="attribute">
<dt id="django.forms.ModelChoiceField.empty_label">
<tt class="descname">empty_label</tt><a class="headerlink" href="#django.forms.ModelChoiceField.empty_label" title="Permalink to this definition">¶</a></dt>
<dd><p>By default the <tt class="docutils literal"><span class="pre">&lt;select&gt;</span></tt> widget used by <tt class="docutils literal"><span class="pre">ModelChoiceField</span></tt> will have an
empty choice at the top of the list. You can change the text of this
label (which is <tt class="docutils literal"><span class="pre">&quot;---------&quot;</span></tt> by default) with the <tt class="docutils literal"><span class="pre">empty_label</span></tt>
attribute, or you can disable the empty label entirely by setting
<tt class="docutils literal"><span class="pre">empty_label</span></tt> to <tt class="docutils literal"><span class="pre">None</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># A custom empty label</span>
<span class="n">field1</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">ModelChoiceField</span><span class="p">(</span><span class="n">queryset</span><span class="o">=...</span><span class="p">,</span> <span class="n">empty_label</span><span class="o">=</span><span class="s">&quot;(Nothing)&quot;</span><span class="p">)</span>

<span class="c"># No empty label</span>
<span class="n">field2</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">ModelChoiceField</span><span class="p">(</span><span class="n">queryset</span><span class="o">=...</span><span class="p">,</span> <span class="n">empty_label</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that if a <tt class="docutils literal"><span class="pre">ModelChoiceField</span></tt> is required and has a default
initial value, no empty choice is created (regardless of the value
of <tt class="docutils literal"><span class="pre">empty_label</span></tt>).</p>
</dd></dl>

<dl class="attribute">
<dt id="django.forms.ModelChoiceField.to_field_name">
<tt class="descname">to_field_name</tt><a class="headerlink" href="#django.forms.ModelChoiceField.to_field_name" title="Permalink to this definition">¶</a></dt>
<dd><p>This optional argument is used to specify the field to use as the value
of the choices in the field&#8217;s widget. Be sure it&#8217;s a unique field for
the model, otherwise the selected value could match more than one
object. By default it is set to <tt class="docutils literal"><span class="pre">None</span></tt>, in which case the primary key
of each object will be used. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># No custom to_field_name</span>
<span class="n">field1</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">ModelChoiceField</span><span class="p">(</span><span class="n">queryset</span><span class="o">=...</span><span class="p">)</span>
</pre></div>
</div>
<p>would yield:</p>
<div class="highlight-html"><div class="highlight"><pre><span class="nt">&lt;select</span> <span class="na">id=</span><span class="s">&quot;id_field1&quot;</span> <span class="na">name=</span><span class="s">&quot;field1&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;option</span> <span class="na">value=</span><span class="s">&quot;obj1.pk&quot;</span><span class="nt">&gt;</span>Object1<span class="nt">&lt;/option&gt;</span>
<span class="nt">&lt;option</span> <span class="na">value=</span><span class="s">&quot;obj2.pk&quot;</span><span class="nt">&gt;</span>Object2<span class="nt">&lt;/option&gt;</span>
...
<span class="nt">&lt;/select&gt;</span>
</pre></div>
</div>
<p>and:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># to_field_name provided</span>
<span class="n">field2</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">ModelChoiceField</span><span class="p">(</span><span class="n">queryset</span><span class="o">=...</span><span class="p">,</span> <span class="n">to_field_name</span><span class="o">=</span><span class="s">&quot;name&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>would yield:</p>
<div class="highlight-html"><div class="highlight"><pre><span class="nt">&lt;select</span> <span class="na">id=</span><span class="s">&quot;id_field2&quot;</span> <span class="na">name=</span><span class="s">&quot;field2&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;option</span> <span class="na">value=</span><span class="s">&quot;obj1.name&quot;</span><span class="nt">&gt;</span>Object1<span class="nt">&lt;/option&gt;</span>
<span class="nt">&lt;option</span> <span class="na">value=</span><span class="s">&quot;obj2.name&quot;</span><span class="nt">&gt;</span>Object2<span class="nt">&lt;/option&gt;</span>
...
<span class="nt">&lt;/select&gt;</span>
</pre></div>
</div>
</dd></dl>

<p>The <tt class="docutils literal"><span class="pre">__str__</span></tt> (<tt class="docutils literal"><span class="pre">__unicode__</span></tt> on Python 2) method of the model will be
called to generate string representations of the objects for use in the
field&#8217;s choices; to provide customized representations, subclass
<tt class="docutils literal"><span class="pre">ModelChoiceField</span></tt> and override <tt class="docutils literal"><span class="pre">label_from_instance</span></tt>. This method will
receive a model object, and should return a string suitable for representing
it. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.forms</span> <span class="kn">import</span> <span class="n">ModelChoiceField</span>

<span class="k">class</span> <span class="nc">MyModelChoiceField</span><span class="p">(</span><span class="n">ModelChoiceField</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">label_from_instance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&quot;My Object #</span><span class="si">%i</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">obj</span><span class="o">.</span><span class="n">id</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-modelmultiplechoicefield">
<span id="modelmultiplechoicefield"></span><h3><tt class="docutils literal"><span class="pre">ModelMultipleChoiceField</span></tt><a class="headerlink" href="#modelmultiplechoicefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.ModelMultipleChoiceField">
<em class="property">class </em><tt class="descname">ModelMultipleChoiceField</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.forms.ModelMultipleChoiceField" title="Permalink to this definition">¶</a></dt>
<dd><ul class="simple">
<li>Default widget: <a class="reference internal" href="widgets.html#django.forms.SelectMultiple" title="django.forms.SelectMultiple"><tt class="xref py py-class docutils literal"><span class="pre">SelectMultiple</span></tt></a></li>
<li>Empty value: An empty <tt class="docutils literal"><span class="pre">QuerySet</span></tt> (self.queryset.none())</li>
<li>Normalizes to: A <tt class="docutils literal"><span class="pre">QuerySet</span></tt> of model instances.</li>
<li>Validates that every id in the given list of values exists in the
queryset.</li>
<li>Error message keys: <tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">list</span></tt>, <tt class="docutils literal"><span class="pre">invalid_choice</span></tt>,
<tt class="docutils literal"><span class="pre">invalid_pk_value</span></tt></li>
</ul>
<div class="versionchanged">
<span class="title">Changed in Django 1.6:</span> <p>The <tt class="docutils literal"><span class="pre">invalid_choice</span></tt> message may contain <tt class="docutils literal"><span class="pre">%(value)s</span></tt> and the
<tt class="docutils literal"><span class="pre">invalid_pk_value</span></tt> message may contain <tt class="docutils literal"><span class="pre">%(pk)s</span></tt>, which will be
substituted by the appropriate values.</p>
</div>
<p>Allows the selection of one or more model objects, suitable for
representing a many-to-many relation. As with <a class="reference internal" href="#django.forms.ModelChoiceField" title="django.forms.ModelChoiceField"><tt class="xref py py-class docutils literal"><span class="pre">ModelChoiceField</span></tt></a>,
you can use <tt class="docutils literal"><span class="pre">label_from_instance</span></tt> to customize the object
representations, and <tt class="docutils literal"><span class="pre">queryset</span></tt> is a required parameter:</p>
<dl class="attribute">
<dt id="django.forms.ModelMultipleChoiceField.queryset">
<tt class="descname">queryset</tt><a class="headerlink" href="#django.forms.ModelMultipleChoiceField.queryset" title="Permalink to this definition">¶</a></dt>
<dd><p>A <tt class="docutils literal"><span class="pre">QuerySet</span></tt> of model objects from which the choices for the
field will be derived, and which will be used to validate the
user&#8217;s selection.</p>
</dd></dl>

</dd></dl>

</div>
</div>
<div class="section" id="s-creating-custom-fields">
<span id="creating-custom-fields"></span><h2>Creating custom fields<a class="headerlink" href="#creating-custom-fields" title="Permalink to this headline">¶</a></h2>
<p>If the built-in <tt class="docutils literal"><span class="pre">Field</span></tt> classes don&#8217;t meet your needs, you can easily create
custom <tt class="docutils literal"><span class="pre">Field</span></tt> classes. To do this, just create a subclass of
<tt class="docutils literal"><span class="pre">django.forms.Field</span></tt>. Its only requirements are that it implement a
<tt class="docutils literal"><span class="pre">clean()</span></tt> method and that its <tt class="docutils literal"><span class="pre">__init__()</span></tt> method accept the core arguments
mentioned above (<tt class="docutils literal"><span class="pre">required</span></tt>, <tt class="docutils literal"><span class="pre">label</span></tt>, <tt class="docutils literal"><span class="pre">initial</span></tt>, <tt class="docutils literal"><span class="pre">widget</span></tt>,
<tt class="docutils literal"><span class="pre">help_text</span></tt>).</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Form fields</a><ul>
<li><a class="reference internal" href="#core-field-arguments">Core field arguments</a><ul>
<li><a class="reference internal" href="#required"><tt class="docutils literal"><span class="pre">required</span></tt></a></li>
<li><a class="reference internal" href="#label"><tt class="docutils literal"><span class="pre">label</span></tt></a></li>
<li><a class="reference internal" href="#initial"><tt class="docutils literal"><span class="pre">initial</span></tt></a></li>
<li><a class="reference internal" href="#widget"><tt class="docutils literal"><span class="pre">widget</span></tt></a></li>
<li><a class="reference internal" href="#help-text"><tt class="docutils literal"><span class="pre">help_text</span></tt></a></li>
<li><a class="reference internal" href="#error-messages"><tt class="docutils literal"><span class="pre">error_messages</span></tt></a></li>
<li><a class="reference internal" href="#validators"><tt class="docutils literal"><span class="pre">validators</span></tt></a></li>
<li><a class="reference internal" href="#localize"><tt class="docutils literal"><span class="pre">localize</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#built-in-field-classes">Built-in <tt class="docutils literal"><span class="pre">Field</span></tt> classes</a><ul>
<li><a class="reference internal" href="#booleanfield"><tt class="docutils literal"><span class="pre">BooleanField</span></tt></a></li>
<li><a class="reference internal" href="#charfield"><tt class="docutils literal"><span class="pre">CharField</span></tt></a></li>
<li><a class="reference internal" href="#choicefield"><tt class="docutils literal"><span class="pre">ChoiceField</span></tt></a></li>
<li><a class="reference internal" href="#typedchoicefield"><tt class="docutils literal"><span class="pre">TypedChoiceField</span></tt></a></li>
<li><a class="reference internal" href="#datefield"><tt class="docutils literal"><span class="pre">DateField</span></tt></a></li>
<li><a class="reference internal" href="#datetimefield"><tt class="docutils literal"><span class="pre">DateTimeField</span></tt></a></li>
<li><a class="reference internal" href="#decimalfield"><tt class="docutils literal"><span class="pre">DecimalField</span></tt></a></li>
<li><a class="reference internal" href="#emailfield"><tt class="docutils literal"><span class="pre">EmailField</span></tt></a></li>
<li><a class="reference internal" href="#filefield"><tt class="docutils literal"><span class="pre">FileField</span></tt></a></li>
<li><a class="reference internal" href="#filepathfield"><tt class="docutils literal"><span class="pre">FilePathField</span></tt></a></li>
<li><a class="reference internal" href="#floatfield"><tt class="docutils literal"><span class="pre">FloatField</span></tt></a></li>
<li><a class="reference internal" href="#imagefield"><tt class="docutils literal"><span class="pre">ImageField</span></tt></a></li>
<li><a class="reference internal" href="#integerfield"><tt class="docutils literal"><span class="pre">IntegerField</span></tt></a></li>
<li><a class="reference internal" href="#ipaddressfield"><tt class="docutils literal"><span class="pre">IPAddressField</span></tt></a></li>
<li><a class="reference internal" href="#genericipaddressfield"><tt class="docutils literal"><span class="pre">GenericIPAddressField</span></tt></a></li>
<li><a class="reference internal" href="#multiplechoicefield"><tt class="docutils literal"><span class="pre">MultipleChoiceField</span></tt></a></li>
<li><a class="reference internal" href="#typedmultiplechoicefield"><tt class="docutils literal"><span class="pre">TypedMultipleChoiceField</span></tt></a></li>
<li><a class="reference internal" href="#nullbooleanfield"><tt class="docutils literal"><span class="pre">NullBooleanField</span></tt></a></li>
<li><a class="reference internal" href="#regexfield"><tt class="docutils literal"><span class="pre">RegexField</span></tt></a></li>
<li><a class="reference internal" href="#slugfield"><tt class="docutils literal"><span class="pre">SlugField</span></tt></a></li>
<li><a class="reference internal" href="#timefield"><tt class="docutils literal"><span class="pre">TimeField</span></tt></a></li>
<li><a class="reference internal" href="#urlfield"><tt class="docutils literal"><span class="pre">URLField</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#slightly-complex-built-in-field-classes">Slightly complex built-in <tt class="docutils literal"><span class="pre">Field</span></tt> classes</a><ul>
<li><a class="reference internal" href="#combofield"><tt class="docutils literal"><span class="pre">ComboField</span></tt></a></li>
<li><a class="reference internal" href="#multivaluefield"><tt class="docutils literal"><span class="pre">MultiValueField</span></tt></a></li>
<li><a class="reference internal" href="#splitdatetimefield"><tt class="docutils literal"><span class="pre">SplitDateTimeField</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#fields-which-handle-relationships">Fields which handle relationships</a><ul>
<li><a class="reference internal" href="#modelchoicefield"><tt class="docutils literal"><span class="pre">ModelChoiceField</span></tt></a></li>
<li><a class="reference internal" href="#modelmultiplechoicefield"><tt class="docutils literal"><span class="pre">ModelMultipleChoiceField</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#creating-custom-fields">Creating custom fields</a></li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="api.html">The Forms API</a></li>
    
    
      <li>Next: <a href="models.html">Model Form Functions</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../index.html">API Reference</a>
        
          <ul><li><a href="index.html">Forms</a>
        
        <ul><li>Form fields</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../_sources/ref/forms/fields.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="api.html" title="The Forms API">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="models.html" title="Model Form Functions">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>